package com.example.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.example.demo.config.properties.CharbotProperties;
import com.example.demo.domain.dto.ChatDTO;
import com.example.demo.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 *
 * @author kisofdeath
 * @date 2022/5/11 12:32
 */
@Service("aiChatService")
public class AiChatServiceImpl implements ChatService {
	@Autowired
	private CharbotProperties charbotProperties;

	/**
	 * 聊天过于温柔，感觉不适用于弹幕交流
	 * @param params
	 * @return
	 */
	@Override
	public String getChatRespText(ChatDTO params) {
		String accountAccessAK = charbotProperties.getAccessKeyId();
		String accountAccessSK = charbotProperties.getAccessSecret();
		String popRegion = charbotProperties.getRegion();
		String popProduct = charbotProperties.getProduct();
		String popDomain = charbotProperties.getPopDomain();
		DefaultProfile.addEndpoint(popRegion, popProduct, popDomain);
		IClientProfile profile = DefaultProfile.getProfile(popRegion, accountAccessAK, accountAccessSK);
		DefaultAcsClient client = new DefaultAcsClient(profile);
		//固定入参
		CommonRequest commonRequest = new CommonRequest();
		commonRequest.setSysProduct(charbotProperties.getProduct());
		commonRequest.setSysMethod(MethodType.GET);
		//根据API会有变化
		commonRequest.setSysAction(charbotProperties.getAction());
		commonRequest.setSysVersion(charbotProperties.getSysVersion());
		commonRequest.putQueryParameter("Utterance", params.getContent());
		//机器人id
		commonRequest.putQueryParameter("InstanceId", charbotProperties.getDefaultInstanceId());
		String resp;
		try {
			CommonResponse commonResponse = client.getCommonResponse(commonRequest);
			resp = commonResponse.getData();
		} catch (ClientException e) {
			throw new RuntimeException(e);
		}
		/*
		{"Messages":
		[{"Type":"Text","AnswerType":"Text","Text":{"Ext":{},"ContentType":"PLAIN_TEXT","UserDefinedChatTitle":"",
		"AnswerSource":"ChitChat","Content":"我一直在这里呀","HitStatement":"hi"},"Knowledge":{}}],
		"RequestId":"??","SessionId":"??",
		"MessageId":"??"}
		 */
		JSONObject respJson = JSON.parseObject(resp);
		String content = "";
		JSONArray messageArr = respJson.getJSONArray("Messages");
		if (messageArr != null && messageArr.size() > 0) {
			JSONObject message = (JSONObject)messageArr.get(0);
			// 闲聊
			JSONObject textObj = message.getJSONObject("Text");
			if (textObj == null || !textObj.containsKey("Content")) {
				// FAQ
				textObj = message.getJSONObject("Knowledge");
			}
			// 后续种类后续增加
			if (textObj != null) {
				String answerSource = textObj.getString("AnswerSource");
				if (!"NO_ANSWER".equals(answerSource)) {
					content = textObj.getString("Content");
				}
			}
		}
		return Optional.ofNullable(content).orElse("你说得对");
	}
}
